package com.hnucm.eatwhat_java.act;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hnucm.eatwhat_java.MainActivity;
import com.hnucm.eatwhat_java.R;
import com.hnucm.eatwhat_java.databinding.ActivityAddBinding;
import com.hnucm.eatwhat_java.pojo.Shop;
import com.hnucm.eatwhat_java.util.GlideEngine;
import com.hnucm.eatwhat_java.util.UtilApplication;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 增加菜品界面
 */
public class AddActivity extends AppCompatActivity {

    private String TAG="AddActivity";
    private ActivityAddBinding binding;
    private Shop shopTemp;
    private String urlImg="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(this,R.layout.activity_add);

        binding.addEatBtn.setOnClickListener(v -> {
            String shopName=binding.getShopName();
            Log.d(TAG, "onClick: "+shopName);
            if(shopName==null||shopName.equals("")||shopName.equals("商品名称")){
                Toast.makeText(AddActivity.this,"输入不合法",Toast.LENGTH_SHORT).show();
            }else if(urlImg.equals("")){
                Toast.makeText(AddActivity.this,"图片为空",Toast.LENGTH_SHORT).show();
            }else{
                shopTemp=new Shop(shopName,urlImg);
                EventBus.getDefault().post(shopTemp);
                finish();
            }
        });
        ImmersionBar.with(this).statusBarDarkFont(true).init();


        //申请权限
        XXPermissions.with(this)
            .permission(Permission.READ_EXTERNAL_STORAGE)
            .permission(Permission.WRITE_EXTERNAL_STORAGE)
            .permission(Permission.RECORD_AUDIO)
            .permission(Permission.CAMERA)
            .request((permissions, all) -> {
                if(!all){
                    Toast.makeText(AddActivity.this,"获取部分权限成功，但部分权限未正常授予",Toast.LENGTH_SHORT).show();
                    return;
                }
            });
        //拍照
        binding.addShopImg.setOnClickListener(v -> PictureSelector.create(AddActivity.this)
                .openCamera(SelectMimeType.ofImage())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        //返回的是一个列表因为可能会选择多张图片
                        //打印图片默认保存的路径
                        Log.d(TAG, "onResult: "+result.get(0).getRealPath());
                        Glide.with(AddActivity.this).
                                load(Uri.fromFile(new File(result.get(0).getRealPath())))
                                .into(binding.addShopImg);
                        urlImg=result.get(0).getRealPath();
//                        Toast.makeText(AddActivity.this,urlImg,Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel() {

                    }
                }));
        //获取相册
        binding.getPic.setOnClickListener(v -> PictureSelector.create(AddActivity.this)
            .openGallery(SelectMimeType.ofImage())
            .setImageEngine(GlideEngine.createGlideEngine())
            .forResult(new OnResultCallbackListener<LocalMedia>() {
                @Override
                public void onResult(ArrayList<LocalMedia> result) {
                    Log.d(TAG, "onResult: "+result.get(0).getRealPath());
                    Glide.with(AddActivity.this).
                            load(Uri.fromFile(new File(result.get(0).getRealPath())))
                            .into(binding.addShopImg);
                    urlImg=result.get(0).getRealPath();
                }

                @Override
                public void onCancel() {

                }
            }));

        //返回
        binding.backBtn.setOnClickListener(v -> finish());
    }
}